package com.shu.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2021/8/30 18:06
 */

public class YieldTest {


    public static void main(String[] args) throws InterruptedException {
        yeildTest();
        System.out.println("------------------------");
//        joinTest();

    }

    /**
     * join相当于插队,B线程中调用A线程的join() 就是让A 插队到b线程前面执行
     */
    static void joinTest() throws InterruptedException {
        System.out.println("main-start");
        Thread thread = new Thread(() -> {
            System.out.println("异步线程start");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("异步线程end");
        });
        thread.start();
        thread.join();
        System.out.println("main线程 runing");
        System.out.println("main-over");
    }

    /**
     * 当前线程成为就绪状态
     *
     * @throws InterruptedException
     */
    static void yeildTest() throws InterruptedException {
        Thread currentThread = Thread.currentThread();
        System.out.println("main-start");
        Thread thread = new Thread(() -> {
            System.out.println("异步线程start");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
            System.out.println("异步线程end");
        });
        thread.start();
        thread.join();
        System.out.println("main线程 runing");
        System.out.println("main-over");
    }
}
